package com.research.performace.business;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.HibernateException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Component;

import com.research.performace.dao.CountryDAO;
import com.research.performace.entities.Country;

@Component
public class InitdataBusiness {

	private static final Logger log = LoggerFactory.getLogger(InitdataBusiness.class);
	
	@Autowired
	private CountryDAO countryDAO;
	
	/**
	 * @return
	 */
	public List<Country> initCountry() {
		String[] codes = new String[]{"vn", "jp", "au", "fr", "en"};
		String[] names = new String[]{"Viet Name", "Japanese", "Australia", "Friench", "English"};
		
		List<Country> countries = new ArrayList<>();
		for (int i = 0; i < codes.length; i++) {
			Country country = new Country(codes[i], names[i]);
			countries.add(country);
		}
		try {
			countryDAO.save(countries);
		} catch (HibernateException | DataIntegrityViolationException e) {
			log.error(e.getMessage());
		}
		return countryDAO.findAll();
	}
	
}
